package com.ml.mall.service.deduct.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.deduct.DeductEditDto;
import com.ml.mall.entity.deduct.SysADeduct;
import com.ml.mall.mapper.deduct.SysADeductMapper;
import com.ml.mall.service.deduct.SysADeductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SysADeductServiceImpl extends ServiceImpl<SysADeductMapper, SysADeduct> implements SysADeductService {

    @Autowired
    private SysADeductMapper sysADeductMapper;

    @Override
    public SysADeduct selectCurDeduct() {
        return sysADeductMapper.selectById(1);
    }

    @Override
    public ApiRestResult handleDeduct(DeductEditDto dto) {
        SysADeduct sysADeduct = new SysADeduct();
        sysADeduct.setId(1L);
        sysADeduct.setDeductName("111未知");
        sysADeduct.setDeductRate(dto.getDeductRate());
        sysADeduct.setCentType(dto.getCentType());
        // deduct表中数据只有一条  后端把id写死为1；只有一条：如果不存在直接创建；存在就修改；
        SysADeduct dbSysADeduct = sysADeductMapper.selectById(sysADeduct);
        if(dbSysADeduct == null){
            sysADeductMapper.insert(sysADeduct);
        }else {
            sysADeductMapper.updateById(sysADeduct);
        }
        return ApiRestResult.ok();
    }

    @Override
    public SysADeduct getCurDeduct() {
        SysADeduct sysADeducts = sysADeductMapper.selectById(1L);
        return sysADeducts;
    }
}
